{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Input From MongoDB\n",
    "This notebook pulls data set from [MongoDB Atlas -- The Cloud-Native Database](https://www.mongodb.com/cloud/atlas/lp/try4?utm_source=google&utm_campaign=search_gs_pl_evergreen_atlas_core-high-int_prosp-brand_gic-null_emea-ie_ps-all_desktop_eng_lead&utm_term=mongodb%20atlas&utm_medium=cpc_paid_search&utm_ad=e&utm_ad_campaign_id=19630910055&adgroup=145923638859&cq_cmp=19630910055&gad=1&gclid=Cj0KCQjwsIejBhDOARIsANYqkD2ewYCphoJvBv_op03rwncSNye4Mq_RihH9EaZKsnFK4BV-roOCOrYaAuSREALw_wcB) database\n",
    "- first connect with mongo with url\n",
    "- then test the connection\n",
    "- save retrieved data into csv file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pymongo, dnspython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymongo import MongoClient\n",
    "import csv\n",
    "import os\n",
    "import logging\n",
    "import sys\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# database username\n",
    "database_username = os.environ.get('database_username')\n",
    "\n",
    "# database password\n",
    "password = os.environ.get('password')\n",
    "\n",
    "# cluster url\n",
    "cluster_url = os.environ.get('cluster_url')\n",
    "\n",
    "# database name\n",
    "database = os.environ.get('database')\n",
    "\n",
    "# database collection\n",
    "collection = os.environ.get('collection')\n",
    "\n",
    "# database document name\n",
    "document = os.environ.get('document')\n",
    "\n",
    "# path and file name for output\n",
    "output_data_csv = os.environ.get('output_data_csv')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = list(\n",
    "    map(lambda s: re.sub('$', '\"', s),\n",
    "        map(\n",
    "            lambda s: s.replace('=', '=\"'),\n",
    "            filter(\n",
    "                lambda s: s.find('=') > -1 and bool(re.match(r'[A-Za-z0-9_]*=[.\\/A-Za-z0-9]*', s)),\n",
    "                sys.argv\n",
    "            )\n",
    "    )))\n",
    "\n",
    "for parameter in parameters:\n",
    "    logging.warning('Parameter: ' + parameter)\n",
    "    exec(parameter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MongoDB connection settings, Connection URL. Where your mongodb server is running.\n",
    "url = f\"mongodb+srv://{database_username}:{password}@{cluster_url}/test?retryWrites=true&w=majority\"\n",
    "\n",
    "# Connect to MongoDB\n",
    "client = MongoClient(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MongoDB connection testing, Access specific database\n",
    "db = client[database]\n",
    "\n",
    "# Access specific collection\n",
    "collection = db[collection]\n",
    "\n",
    "# Retrieve a document from the collection\n",
    "document = collection.find_one(document)\n",
    "\n",
    "# Print/Show the document\n",
    "print(document)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the document, Open the file in write mode\n",
    "with open(output_data_csv, 'w', newline='') as file:\n",
    "    # Create a CSV writer object\n",
    "    writer = csv.writer(file)\n",
    "\n",
    "    # Write the document data to the CSV file\n",
    "    writer.writerow(document.keys())  # Write the header row\n",
    "    writer.writerow(document.values())  # Write the data row\n",
    "\n",
    "# Close the file\n",
    "file.close()\n",
    "\n",
    "print(\"Output saved successfully to CSV!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#finally close the connection\n",
    "client.close()\n",
    "print(\"Output saved successfully to CSV!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
